
/*
 * Copyright (c) 2016 moondev.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.moondev.moonlog.example.moonlog.monitor.service.impl;

import java.util.List;
import java.util.Map;

import cn.moondev.moonlog.example.moonlog.monitor.dao.BizMonitorDao;
import cn.moondev.moonlog.example.moonlog.monitor.entity.BizMonitor;
import cn.moondev.moonlog.example.moonlog.monitor.service.BizMonitorService;
import cn.moondev.moonlog.example.moonlog.monitor.utils.Page;
import cn.moondev.moonlog.example.moonlog.monitor.utils.TableBuilder;
import org.joda.time.DateTime;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service("bizMonitorService")
public class BizMonitorServiceImpl implements BizMonitorService {
	
	@Autowired
	BizMonitorDao bizMonitorDao;

	@Override
	public Page<BizMonitor> list(String sys, int index, int size, String start, String end) {
		// 验证数据
		Preconditions.checkNotNull(sys,"sys参数不能为空");
		Preconditions.checkNotNull(start,"start参数不能为空");
		Preconditions.checkNotNull(end,"end参数不能为空");
		if (size <= 0) {
			size = 10;
		}
		if(index <=0) {
			index = 1;
		}
		Page<BizMonitor> page = new Page<BizMonitor>(index, size);
		String tableName = TableBuilder.buildBizMonitorTableName(new DateTime());
		// 2015-12-12 12:12:12格式日期 ---> 毫秒
		long start_ms = new DateTime(start.replace(" ", "T")).getMillis();
		long end_ms = new DateTime(end.replace(" ", "T")).getMillis();
		List<BizMonitor> list = bizMonitorDao.list(sys, start_ms, end_ms,tableName, page.getFirst(), size);
		long total = bizMonitorDao.count(sys, start_ms, end_ms, tableName);
		page.setTotalCount(total);
		page.setResult(list);
		return page;
	}

	@Override
	public Page<BizMonitor> listByDate(String sys, int index, int size, String date) {
		// 验证数据
		Preconditions.checkNotNull(sys,"sys参数不能为空");
		if (size <= 0) {
			size = 10;
		}
		if(index <=0) {
			index = 1;
		}
		Page<BizMonitor> page = new Page<BizMonitor>(index, size);
		String tableName = TableBuilder.buildBizMonitorTableName(new DateTime());
		List<BizMonitor> list = bizMonitorDao.listByDate(sys, date, tableName, page.getFirst(), size);
		long total = bizMonitorDao.countByDate(sys, date, tableName);
		page.setTotalCount(total);
		page.setResult(list);
		return page;
	}

	@Override
	public Page<Map<String,Object>> calcInvokeTimes(String sys, int index, int size, String start, String end, String date) {
		// 验证数据
		Preconditions.checkNotNull(sys,"sys参数不能为空");
		if (size <= 0) {
			size = 10;
		}
		if(index <=0) {
			index = 1;
		}
		Page<Map<String,Object>> page = new Page<Map<String,Object>>(index,size);
		String tableName = TableBuilder.buildBizMonitorTableName(new DateTime());
		// 2015-12-12 12:12:12格式日期 ---> 毫秒
		long start_ms = 0,end_ms = 0;
		if(StringUtils.isEmpty(date)) { 
			start_ms = new DateTime(start.replace(" ", "T")).getMillis();
			end_ms = new DateTime(end.replace(" ", "T")).getMillis();
		}
		List<Map<String,Object>> list = bizMonitorDao.calcInvokeTimes(sys, page.getFirst(), size, start_ms, end_ms, date, tableName);
		long total = bizMonitorDao.countInvokeTimes(sys, start_ms, end_ms, date, tableName);
		page.setTotalCount(total);
		page.setResult(list);
		return page;
	}
}
